<?php

class Model_Content extends Model_Entities_Content
{

    public  function buildAttributes($params) {
        $auth = Zend_Auth::getInstance()->getStorage('Administrator');
        $userProfile = $auth->read();
        $attributes = array();
        $attributes['section_id'] = $params['section_id'];
        $attributes['category_id'] = $params['category_id'];
        $attributes['introtext'] = $params['introtext'];
        $attributes['fulltext'] = $params['fulltext'];
        $attributes['user_id'] = $userProfile['id'];
        if(isset($params['id']) && $params['id'] > 0) {
            $attributes['id'] = $params['id'];
            $attributes['modified_date'] = date('Y-m-d H:i:s');
        }
        $attributes['name'] = $params['name'];
        if(isset($params['images'])) {
            $attributes['images'] = $params['images'];
        }
        if($params['published'] == 'yes') {
            $attributes['published'] = 1;
        }
        else {
            $attributes['published'] = '0';
        }
        return $attributes;
    }

    public function updatePublished($ids, $value) {
        $mapper = $this->getMapper();
        $dbTable = $mapper->getDbTable();
        $data = array('published' => intval($value));
        if(is_array($ids)) {
            $where = 'id IN ('.implode(', ', $ids).')';
        }
        else if($ids > 0){
            $where = 'id IN ('.$ids.')';
        }
        $numRows = $dbTable->update($data, $where);

        return $numRows;
    }

    /**
     * Enter description here ...
     * @param int $page
     * @param int $limit
     * @param array $wheres
     * @param array $orders
     * @param array $orWheres
     * @param array $likes
     * * @param array $orLikes
     * @return array
     */
    public function getListItems($page = 0, $limit = 0, $wheres = array(), $orders = array('DESC' => 'created_date'), $orWheres = array(), $likes = array(), $orLikes = array()) {
        $mapper = $this->getMapper();
        $dbTable = $mapper->getDbTable();
        $select = $dbTable->getDefaultAdapter()->select();
        $select->from(array('content' => $dbTable->getTableName()));
        $select->join(array('category' => $this->getTablePrefix().'category'), 'category.id = content.category_id', array('category_name' => 'category.name'));
        $select->join(array('section' => $this->getTablePrefix().'section'), 'section.id = category.section_id AND section.id = content.section_id', array('section_name' => 'section.name'));

        if ($limit > 0) {
            $select->limitPage($page, $limit);
        }

        if (is_array($orders) && count($orders) > 0) {
            foreach ($orders as $key => $order) {
                $select->order($order);
            }
        } elseif (is_string($orders) && !empty($orders)) {
            $select->order($orders);
        }

        if (is_array($wheres) && count($wheres) > 0) {
            foreach ($wheres as $key => $where) {
                if (!empty($key) && !intval($key)) {
                    $select->where($key.' = ?', $where);
                }
                else {
                    $select->where($where);
                }
            }
        } elseif (is_string($wheres) && !empty($wheres)) {
            $select->where($wheres);
        }

        if (is_array($orWheres) && count($orWheres) > 0) {
            foreach ($orWheres as $key => $where) {
                if (!empty($key) && !intval($key)) {
                    $select->orWhere($key.' = ?', $where);
                }
            }
        }

        if (is_array($likes) && count($likes) > 0) {
            foreach ($likes as $key => $where) {
                if (!empty($key) && !intval($key)) {
                    $select->where($key.' LIKE ?', $where);
                }
            }
        }

        if (is_array($orLikes) && count($orLikes) > 0) {
            foreach ($orLikes as $key => $where) {
                if (!empty($key) && !intval($key)) {
                    $select->orWhere($key.' LIKE ?', $where);
                }
            }
        }

        //Zend_Debug::dump($select->__toString());die;
        $rst = $dbTable->getDefaultAdapter()->fetchAll($select);
        return $rst;
    }

    /**
     * Enter description here ...
     * @param array $wheres
     * @param array $orWheres
     * @param array $likes
     * @param array $orLikes
     * @return int
     */
    public function getTotal($wheres = array(), $orWheres = array(), $likes = array(), $orLikes = array())
    {
        $mapper = $this->getMapper();
        $dbTable = $mapper->getDbTable();
        $select = $dbTable->getDefaultAdapter()->select();
        $select->from(array('content' => $dbTable->getTableName()), array('CNT' => 'count(DISTINCT content.id)'));
        $select->join(array('category' => $this->getTablePrefix().'category'), 'category.id = content.category_id', array());
        $select->join(array('section' => $this->getTablePrefix().'section'), 'section.id = category.section_id AND section.id = content.section_id', array());

        if (is_array($wheres) && count($wheres) > 0) {
            foreach ($wheres as $key => $where) {
                if (!empty($key) && !intval($key)) {
                    $select->where($key.' = ?', $where);
                }
                else {
                    $select->where($where);
                }
            }
        } elseif (is_string($wheres) && !empty($wheres)) {
            $select->where($wheres);
        }

        if (is_array($orWheres) && count($orWheres) > 0) {
            foreach ($orWheres as $key => $where) {
                if (!empty($key) && !intval($key)) {
                    $select->orWhere($key.' = ?', $where);
                }
            }
        }

        if (is_array($likes) && count($likes) > 0) {
            foreach ($likes as $key => $where) {
                if (!empty($key) && !intval($key)) {
                    $select->where($key.' LIKE ?', $where);
                }
            }
        }

        if (is_array($orLikes) && count($orLikes) > 0) {
            foreach ($orLikes as $key => $where) {
                if (!empty($key) && !intval($key)) {
                    $select->orWhere($key.' LIKE ?', $where);
                }
            }
        }

        $result = $dbTable->getDefaultAdapter()->fetchRow($select);
        return $result['CNT'];
    }
}